<!--
 * @Author: your name
 * @Date: 2021-08-26 12:26:56
 * @LastEditTime: 2021-08-29 19:20:56
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue电商管理平台\vue-e-commerce\src\components\goods\List.vue
-->


<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="queryInfo.query = ''"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button> </el-input
        ></el-col>
        <el-col :span="4"
          ><el-button type="primary" @click="goAddGoods"
            >添加商品</el-button
          ></el-col
        >
      </el-row>
      <!-- 数据表 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          label="商品数量"
          prop="goods_number"
          width="80px"
        ></el-table-column>
        <el-table-column
          label="商品价格（元）"
          prop="goods_price"
          width="80px"
        ></el-table-column>
        <el-table-column
          label="商品重量（kg）"
          prop="goods_weight"
          width="80px"
        ></el-table-column>
        <el-table-column label="创建时间" width="160px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template></el-table-column
        >
        <el-table-column label="操作" width="120px"
          ><template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditGoods(scope.row.goods_id)"
            ></el-button
            ><el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeGoods(scope.row.goods_id)"
            ></el-button> </template
        ></el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 编辑的对话框 -->
    <el-dialog title="编辑" :visible.sync="editDialogVisible" width="50%">
      <el-form
        :model="editForm"
        :rules="rules"
        ref="editFormRef"
        label-width="100px"
      >
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="editForm.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoods">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //查询参数
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      goodsList: [],
      total: 0,
      // 编辑的对话框
      editDialogVisible: false,
      // 编辑 对话框 的查询数据
      editForm: {},
      rules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
        ],
        goods_number: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    editId() {
      return this.editForm.goods_id;
    },
    editInfo() {
      return {
        goods_name: this.editForm.goods_name,
        goods_price: this.editForm.goods_price,
        goods_number: this.editForm.goods_number,
        goods_weight: this.editForm.goods_weight,
      };
    },
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$axios.get("goods", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        return this.$message.error("商品列表查询失败");
      }
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    //处理 分页器 展示数据量
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodsList();
    },
    // 处理 分页器 当前页
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoodsList();
    },
    //删除商品
    async removeGoods(id) {
      const confirmResult = await this.$confirm("是否删除该商品?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch((err) => err);
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }
      const { data: res } = await this.$axios.delete("goods/" + id);
      if (res.meta.status !== 200) {
        return this.$message.error("商品删除失败");
      }
      this.$message.success("商品删除成功");
      this.getGoodsList();
    },
    //跳转到添加商品页面
    goAddGoods() {
      this.$router.push("goods/add");
    },
    // 监听 编辑 对话框 打开
    async showEditGoods(id) {
      const { data: res } = await this.$axios.get("goods/" + id);
      if (res.meta.status !== 200) {
        return this.$message.error("商品查询失败");
      }
      this.editForm = res.data;
      this.editDialogVisible = true;
    },
    // 编辑商品
    editGoods() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$axios.put(
          "goods/" + this.editId,
          this.editInfo
        );
        if (res.meta.status !== 201) {
          return this.$message.error("修改商品失败");
        }
        this.getGoodsList();
        this.$message.success("修改商品成功");
        this.editDialogVisible = false;
      });
    },
  },
  created() {
    this.getGoodsList();
  },
};
</script>

<style scoped>
</style>